package testcase;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;

import controller.FileIOController;
import controller.MemberController;
import dataStructure.LinkedList;
import entity.FileAsso;
import entity.Member;

public class TestMemberController {
		private LinkedList<Member> list = new LinkedList<Member>();
		private MemberController memberController;
		private FileIOController fileIO = new FileIOController();
		Date bDay;

		public TestMemberController() throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException,
				InvocationTargetException, ParseException, NullPointerException, IOException {
			
			fileIO.getFileAssoList().addContent(new FileAsso("member", "member.txt"));
			fileIO.inputData(list, "member");
			memberController = new MemberController(list, null);
			
			String temp = "1900-01-01";
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-mm-dd");
			bDay= formatter.parse(temp);
			
		}
	@Test
	// correct input 
	public void TestAddMember() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ParseException, InvocationTargetException,
			NoSuchMethodException, IOException {

		memberController.addNewMember("A1234567", "Chan", "TaiMan", bDay, "CityU", 23456789, "M", "000001", new Date(), 0, 100, 0, "1");

		Member expect = new Member("A1234567", "Chan", "TaiMan", bDay, "CityU", 23456789, "M", "000001", new Date(), 0, 100, 0, "1");
		Member result = memberController.getMemberList().getLastNodeContent();
		
		assertEquals(expect.getContentStr(), result.getContentStr());
	}
 
	@Test
	// wrong HKID
	public void TestAddMember2() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ParseException, InvocationTargetException,
			NoSuchMethodException, IOException {

		memberController.addNewMember("A234567", "Chan", "TaiMan", bDay, "CityU", 23456789, "M", "000001", new Date(), 0, 100, 0, "0");

		Member result = memberController.getMemberList().getLastNodeContent();

		assertEquals(null, result.getContentStr());
	}

	@Test
	// wrong last name
	public void TestAddMember3() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ParseException, InvocationTargetException,
			NoSuchMethodException, IOException {

		memberController.addNewMember("A234567", "000", "TaiMan", bDay, "CityU", 23456789, "M", "000001", new Date(), 0, 100, 0, "0");

		Member result = memberController.getMemberList().getLastNodeContent();

		assertEquals(null, result.getContentStr());
	}
	
	@Test
	// wrong first name
	public void TestAddMember4() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ParseException, InvocationTargetException,
			NoSuchMethodException, IOException {

		memberController.addNewMember("A234567", "Chan", "111111", bDay, "CityU", 23456789, "M", "000001", new Date(), 0, 100, 0, "0");

		Member result = memberController.getMemberList().getLastNodeContent();

		assertEquals(null, result.getContentStr());
	}
	
	@Test
	// wrong school name
	public void TestAddMember5() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ParseException, InvocationTargetException,
			NoSuchMethodException, IOException {

		memberController.addNewMember("A234567", "Chan", "TaiMan", bDay, "1234", 23456789, "M", "000001", new Date(), 0, 100, 0, "0");

		Member result = memberController.getMemberList().getLastNodeContent();

		assertEquals(null, result.getContentStr());
	}
	
	@Test
	// wrong contact no.
	public void TestAddMember6() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ParseException, InvocationTargetException,
			NoSuchMethodException, IOException {

		memberController.addNewMember("A234567", "Chan", "TaiMan", bDay, "CityU", 2345678, "M", "000001", new Date(), 0, 100, 0, "0");

		Member result = memberController.getMemberList().getLastNodeContent();

		assertEquals(null, result.getContentStr());
	}
	
	@Test
	// wrong gender
	public void TestAddMember7() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ParseException, InvocationTargetException,
			NoSuchMethodException, IOException {

		memberController.addNewMember("A234567", "Chan", "TaiMan", bDay, "CityU", 23456789, "K", "000001", new Date(), 0, 100, 0, "0");

		Member result = memberController.getMemberList().getLastNodeContent();

		assertEquals(null, result.getContentStr());
	}
	
	@Test
	public void TestAddMemberFromFile() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException,
			InvocationTargetException, ParseException, IOException {
		
		String result = memberController.getMemberList().getLastNodeContent().getContentStr();
		
		assertEquals("K1123334 Chan SiuHong 1990-07-02 CityU 23678909 F; 000002 2013-01-30 90 1999.0 0 false", result);
	}
	
	
	
	@Test
	// correct member ID
	public void TestSuspendMember() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException,
			InvocationTargetException, ParseException, IOException {
		
		Boolean result;
		result = memberController.suspendMember("000001");
		assertEquals(true, result);
	}
	
	@Test
	// incorrect member ID
	public void TestSuspendMember1() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException,
			InvocationTargetException, ParseException, IOException {
		
		Boolean result;
		result = memberController.suspendMember("000009");
		assertEquals(false, result);
	}
	
	@Test
	// null member ID
	public void TestSuspendMember2() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException,
			InvocationTargetException, ParseException, IOException {
		
		Boolean result;
		result = memberController.suspendMember("");
		assertEquals(false, result);
	}
	
	
	@Test
	// correct member id 
	public void TestSearchMember() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException,
			InvocationTargetException, ParseException, IOException {
		
		Member result = memberController.searchMember("000001");
		Member expect = new Member("A1234567", "Chan", "TaiMan", bDay, "CityU", 23456789, "M", "000001", new Date(), 0, 100, 0, "1");
		
		assertEquals("A1234567 Chan TaiMan 1900-01-01 CityU 23456789 M; 000001 2012-12-31 99 100.0 0 false", result.getContentStr());
	}
	
	@Test
	// null member id 
	public void TestSearchMember1() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException,
			InvocationTargetException, ParseException, IOException {

		assertEquals(null, memberController.searchMember(""));
	}
	@Test
	// wrong member id 
	public void TestSearchMember2() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException,
			InvocationTargetException, ParseException, IOException {

		assertEquals(null, memberController.searchMember("000004"));
	}
	
	
}
